<template>

  <div
    style="width:100%;height:360px;"
    id="smoothLine"
  ></div>

</template>

<script>
import echarts from "echarts";

export default {
  name: "dataSourcePie",
  props: {
    LineDataSource: {
      type: Array,
      default: function() {
        return [];
      }
    }
  },
  data() {
    return {};
  },
  watch: {
    LineDataSource(curVal, oldVal) {
      this.initEchats();
    }
  },
  mounted() {},
  methods: {
    initEchats() {
      var LineDataSource = echarts.init(document.getElementById("smoothLine"));
      var fullMonthArray = [
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月"
      ];
      var nowMonth = new Date().getMonth() + 1;
      var xAxisData = fullMonthArray.slice(0, nowMonth);
      var breakData = [];
      var fireData = [];
      var preData = [];
      for (let i = 0; i < nowMonth; i++) {
        let dataItem = this.LineDataSource.find(val => val.mon == i + 1);
        breakData.push(dataItem.break_count);
        fireData.push(dataItem.fire_count);
        preData.push(dataItem.pre_count);
      }

      let option = {
        color: ["#248888", "#ed5485", "#f0d879"],
        tooltip: {
          trigger: "axis"
        },
        legend: {
          data: ["故障", "火警", "预警"]
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xAxisData
        },
        yAxis: {
          type: "value",
          interval: 1,
          axisLabel: {
            formatter: "{value}"
          },
          axisLine: {
            //---坐标轴 轴线
            show: true, //---是否显示
            //------------------- 箭头 -------------------------
            symbol: ["none", "arrow"], //---是否显示轴线箭头
            symbolSize: [8, 8], //---箭头大小
            // symbolOffset: [0, 7], //---箭头位置

            //------------------- 线 -------------------------
            lineStyle: {
              color: "#7f8081",
              width: 1,
              type: "solid"
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: "dashed"
            }
          }
        },
        series: [
          {
            name: "故障",
            type: "line",
            data: breakData,
            smooth: true
          },
          {
            name: "火警",
            type: "line",
            data: fireData,
            smooth: true
          },
          {
            name: "预警",
            type: "line",
            data: preData,
            smooth: true
          }
        ]
      };

      LineDataSource.setOption(option);
      window.addEventListener("resize", function() {
        LineDataSource.resize();
      });
    }
  }
};
</script>
